import { createRouter, createWebHistory } from "vue-router";
import AboutPage from "../views/AboutPage.vue";
import ApiPage from "../views/ApiPage.vue";
import TestPage from "@/views/TestPage.vue";
import LoginPage from "../views/LoginPage.vue";
import HomePage from "@/views/HomePage.vue";
import ConversationPage from "@/views/ConversationPage.vue";
import ManagementPage from "@/views/ManagementPage.vue";
import KnowledgePage from "@/views/KnowledgePage.vue";
import DocumentPage from "@/views/DocumentPage.vue";
import FeedbackPage from "@/views/FeedbackPage.vue";
import TestLiang from "@/components/knowledge/TestLiang.vue";
import NotFound from "@/components/NotFound.vue";
import ContentList from "@/components/knowledge/ContentList.vue";
import { jwtDecode } from "jwt-decode";

const routes = [
  {
    path: "/",
    name: "Home",
    component: HomePage, // 异步懒加载路由提升加载效率 请求省流量
    meta: { hideNavbar: true }, // 隐藏导航栏
  },
  {
    path: "/conversation",
    name: "Conversation",
    component: ConversationPage,
    meta: { requiresAuth: true }, // 需要登录
  },
  {
    path: "/management",
    name: "Management",
    component: ManagementPage,
    meta: { requiresAuth: true }, // 需要登录
  },
  {
    path: "/knowledge",
    name: "Knowledge",
    redirect: "/knowledge/cprompt",
    component: KnowledgePage,
    meta: { requiresAuth: true }, // 需要登录
    children: [
      {
        name: "cprompt",
        path: "cprompt",
        component: ContentList,
      },
    ],
  },
  {
    path: "/document",
    name: "Document",
    component: DocumentPage,
    meta: { requiresAuth: true }, // 需要登录
  },
  {
    path: "/feedback",
    name: "Feedback",
    component: FeedbackPage,
    meta: { requiresAuth: true }, // 需要登录
  },
  {
    path: "/test",
    name: "Test",
    component: TestPage,
    meta: { requiresAuth: true }, // 需要登录
  },
  {
    path: "/testliang",
    name: "TestLiang",
    component: TestLiang,
    meta: { requiresAuth: true }, // 需要登录
  },
  {
    path: "/NotFound", // 非法路由均为404
    component: NotFound,
  },
  {
    name: "404",
    path: "/:pathMatcher(.*)*", // 可以匹配剩余的路径
    redirect: "/NotFound",
  },
  {
    name: "login",
    path: "/login",
    component: LoginPage,
    meta: { hideNavbar: true }, // 隐藏导航栏
  },
];

const router = createRouter({
  history: createWebHistory(),
  routes,
});

function isTokenExpired(token) {
  try {
    const decodedToken = jwtDecode(token, { complete: true });
    const nowInSeconds = Math.floor(Date.now() / 1000); // 转换为秒
    return decodedToken.exp < nowInSeconds;
  } catch (error) {
    // 如果 token 无法解析或者格式不正确，返回 true 表示过期
    return true;
  }
}
// 全局导航守卫
router.beforeEach((to, from, next) => {
  if (to.matched.some((record) => record.meta.requiresAuth)) {
    // localStorage.removeItem("localUser");
    const lu = localStorage.getItem("localUser");
    // 检查是否有本地存储
    if (!lu) {
      next({ name: "login" });
    } else {
      // 检查本地存储的合法性
      const localUser = JSON.parse(lu);
      if (localUser.access_token.length === 0) next({ name: "login" });
      else {
        let token = localUser.access_token;
        if (isTokenExpired(token)) next({ name: "login" });
        else next();
      }
    }
  } else {
    // 不需要认证的路由，直接访问
    next();
  }
});

export default router;
